import Vue from 'vue';
import router from '../router';
import axios from 'axios';
// @ts-ignore
Vue.use(router);
/* Take the baseurl from the configuration file */
// @ts-ignore
axios.defaults.withCredentials = false
let baseUrl = '';
let myAxios = axios.create({
  baseURL: baseUrl,
  timeout: 60000,
});
function encodeMap(map){
  let uri = []
  Object.keys(map).forEach(key => {
    let str = key + '=' + map[key]
    uri.push(str)
  })
  return uri.join('&')
}
function makeFormData(obj, form_data) {
  var data = [];
  if (obj instanceof File)
  {
    data.push({key: "", value: obj});
  }
  else if (obj instanceof Array )
  {
    for (var j=0,len=obj.length;j<len;j++)
    {
      // @ts-ignore
      var arr = makeFormData(obj[j]);
      for (var k=0,l=arr.length;k<l;k++)
      {
        var key = !!form_data ? j+arr[k].key : "["+j+"]"+arr[k].key;
        data.push({key: key, value: arr[k].value})
      }
    }
  }
  else if (typeof obj == 'object')
  {
    // @ts-ignore
    for (var j in obj)
    {
      // @ts-ignore
      var arr = makeFormData(obj[j]);
      for (var k=0,l=arr.length;k<l;k++)
      {
        var key = !!form_data ? j+arr[k].key : "["+j+"]"+arr[k].key;
        data.push({key: key, value: arr[k].value})
      }
    }
  }
  else
  {
    data.push({key: "", value: obj});
  }
  if (!!form_data)
  {
    // 封装
    for (var i=0,len=data.length;i<len;i++)
    {
      form_data.append(data[i].key, data[i].value)
    }
  }
  else
  {
    return data;
  }
}
export class wxios {
  constructor() {
  }
  /* Initialize the request configuration */
  static request(map) {
    let requestConfig = {
      method: map.method,
      url: map.url,
      data: map.method === 'POST' || map.method === 'PUT' ? JSON.stringify(map.data) : null,
      params: map.method === 'GET' || map.method === 'DELETE' ? map.data : null,
      responseType: map.responseType ? map.responseType : 'json',
      headers: map.header ? map.header : { 'Content-Type': 'application/json;charset=utf-8' }
    };
    return new Promise(resolve => {
      myAxios(requestConfig)
        .then(res => {
          map.success && map.success(res);
        })
        .catch(err => {
          map.fail && map.fail(err);
        })
        .then(res => {
          map.complete && map.complete(res);
          resolve();
        });
    });
  }
  static submit(map) {
    let formdata = new FormData()
    makeFormData(map.data,formdata)
    let requestConfig = {
      method: map.method,
      url: map.url,
      data: map.method === 'POST' || map.method === 'PUT' ? formdata : null,
      params: map.method === 'GET' || map.method === 'DELETE' ? formdata : null,
      responseType: map.responseType ? map.responseType : 'json',
      headers: map.header ? map.header : { 'Content-Type': 'multipart/form-data' }
    };
    return new Promise(resolve => {
      myAxios(requestConfig)
        .then(res => {
          map.success(res);
        })
        .catch(err => {
          map.fail(err);
        })
        .then(res => {
          map.complete(res);
          resolve()
        });
    });
  }
  static requestURI(map) {
    let requestConfig = {
      method: map.method,
      url: map.url,
      data: map.method === 'POST' || map.method === 'PUT' ? encodeMap(map.data) : null,
      params: map.method === 'GET' || map.method === 'DELETE' ? encodeMap(map.data) : null,
      responseType: map.responseType ? map.responseType : 'json',
      headers: map.header ? map.header : { 'Content-Type': 'application/x-www-form-urlencoded' }
    };
    return new Promise(resolve => {
      myAxios(requestConfig)
        .then(res => {
          map.success(res);
        })
        .catch(err => {
          map.fail(err);
        })
        .then(res => {
          map.complete(res);
          resolve()
        });
    });
  }
}
